<template>
  <div class="app-container">
    <el-card>
      <el-form ref="searchForm" :model="searchForm" size="mini" :inline="true">
        <div>
          <el-form-item label="企业名称" prop="taxpayerName">
            <el-input style="width: 200px" v-model="searchForm.taxpayerName" placeholder="请输入企业名称" maxlength="200"
              clearable />
          </el-form-item>

          <el-form-item label="税号" prop="creditCode">
            <el-input style="width: 200px" v-model="searchForm.creditCode" maxlength="200" clearable
              placeholder="请输入税号" />
          </el-form-item>
          <el-form-item label="法人姓名" prop="legalPersonName">
            <el-input style="width: 200px" v-model="searchForm.legalPerson" maxlength="200" placeholder="请输入法人姓名"
              clearable />
          </el-form-item>
          <el-form-item label="所属经理">
            <el-select style="width: 200px" v-model="searchForm.adminId" clearable placeholder="请选择所属经理" filterable>
              <el-option v-for="item in managerList" :key="item.id" :label="item.name + ' ' + '(' + item.phone + ')'"
                :value="item.id" />
            </el-select>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="所属省份">
            <el-select clearable style="width: 200px" v-model="searchForm.provinceId" placeholder="请选择所属省份">
              <el-option v-for="item in provinceList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="添加时间">
            <el-date-picker clearable style="width: 200px" v-model="searchForm.beginTime" align="right" type="date"
              placeholder="选择开始日期" :picker-options="pickerOptions" value-format="yyyy-MM-dd" format="yyyy 年 MM 月 dd 日">
            </el-date-picker>
          </el-form-item>
          <el-form-item>-</el-form-item>
          <el-form-item>
            <el-date-picker clearable v-model="searchForm.endTime" align="right" type="date" placeholder="选择结束日期"
              :picker-options="pickerOptions" value-format="yyyy-MM-dd" format="yyyy 年 MM 月 dd 日">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-refresh" @click="resetList">重置</el-button>
            <el-button icon="el-icon-search" type="primary" @click="searchList('searchForm')">查询</el-button>
          </el-form-item>
        </div>
      </el-form>
      <div style="display: flex">
        <router-link v-btnpermission="$btnPer.customerEnterpiseBtnrules.addCustomer"
          :to="{ path: '/createdEnterpiseCustomer' }">
          <el-button size="mini" type="primary" icon="el-icon-plus">新建企业客户</el-button>
        </router-link>
        <el-button v-btnpermission="$btnPer.customerEnterpiseBtnrules.batchDelete" style="margin-left: 10px" size="mini"
          icon="el-icon-delete" @click="deleteMany()">批量删除</el-button>
      </div>
      <div style="margin-top: 20px">
        <el-table :data="tableData" size="mini" @selection-change="selectTableList" v-loading="listLoading"
          :default-sort="{ prop: 'date', order: 'descending' }" border>
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column label="编号" width="100" align="center" type="index"></el-table-column>
          <el-table-column align="center" label="公司名称" prop="taxpayerName"></el-table-column>
          <el-table-column align="center" label="税号" prop="creditCode"></el-table-column>
          <el-table-column align="center" label="法人" prop="legalPerson"></el-table-column>
          <el-table-column align="center" label="所属省份" prop="provinceName"></el-table-column>
          <el-table-column align="center" label="客户经理" prop="adminNickName"></el-table-column>
          <el-table-column sortable align="center" label="添加时间" prop="createTime"></el-table-column>

          <el-table-column align="center" label="获取数据状态">
            <template slot-scope="scope">
              <el-tag v-if="
                scope.row.queryStatus == 0 ||
                scope.row.queryStatus == null ||
                scope.row.queryStatus == -1
              " type="info" size="mini">未获取</el-tag>
              <el-tag v-if="scope.row.queryStatus == 100" size="mini">获取中</el-tag>
              <el-tag v-if="scope.row.queryStatus == 200" type="success" size="mini">获取成功</el-tag>
              <el-tag v-if="scope.row.queryStatus == 300" type="danger" size="mini">获取失败</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="detail(scope.row)" size="small">详情</el-button>
              <router-link v-btnpermission="$btnPer.customerEnterpiseBtnrules.editCustomer"
                :to="{ path: '/createdEnterpiseCustomer', query: { id: scope.row.id } }, ">
                <el-button type="text" size="small">修改</el-button>
              </router-link>
              <el-button v-btnpermission="$btnPer.customerEnterpiseBtnrules.deleteCustomer
                " size="small" @click="deleteItem(scope.row)" type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination class="pageRight" background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 40]" :current-page="searchForm.pageNum" :page-size="searchForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <enterpriseDetail ref="detailRef"></enterpriseDetail>
  </div>
</template>

<script>
import enterpriseDetail from "../components/enterpriseDetail.vue";
import { getUserList } from "@/api/dept";
import {
  getCompanyCustomerList,
  deleteCompanyCustomer
} from "../../../api/customer";
import { getProvinceList } from "@/api/tax";
import { timeFun } from "../../../utils/date";
import { datePickerOptions } from "../options";
export default {
  components: {
    enterpriseDetail
  },
  data() {
    return {
      searchForm: {
        taxpayerName: "",
        creditCode: "",
        legalPersonName: "",
        adminId: "",
        provinceId: "",
        beginTime: "",
        beginTime: "",
        pageNum: 1,
        pageSize: 10
      },
      managerList: [],
      provinceList: [],
      pickerOptions: datePickerOptions,
      listLoading: false,
      tableData: [],
      total: 0,
      deleteList: []
    };
  },
  created() {
    const p1 = getProvinceList();
    const p2 = getUserList();
    Promise.all([p1, p2]).then(res => {
      this.managerList = res[1].data.map(v => {
        return {
          name: v.nickName,
          id: v.adminId,
          phone: v.phoneNumber
        };
      });
      this.provinceList = res[0].data;
      this.getlist();
    });
  },
  methods: {
    getUserList() {
      getUserList().then(res => {
        this.managerList = res.data.map(v => {
          return {
            name: v.nickName,
            id: v.adminId,
            phone: v.phoneNumber
          };
        });
      });

      console.log(this.managerList);
    },
    selectTableList(val) {
      // console.log(val);
      this.deleteList = val.map(v => v.id);
    },
    /**
     * 处理分页大小的改变事件。
     * @param {number} val - 新的分页大小值。
     * 更新搜索表单中的每页记录数，并重新获取列表数据。
     */
    handleSizeChange(val) {
      this.searchForm.pageSize = val; // 更新搜索表单的每页记录数
      this.getlist(); // 重新获取列表数据
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val;
      this.getlist();
    },
    deleteItem(val) {
      let that = this;
      this.$confirm("是否确认删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteCompanyCustomer(val.id).then(res => {
          if (res.code == 200) {
            that.$message({
              type: "success",
              message: "删除成功!"
            });
            setTimeout(() => {
              that.getlist();
            }, 500);
          }
        });
      });
    },
    detail(row) {
      this.$refs.detailRef.opendialog(row);
    },
    deleteMany() {
      let that = this;
      if (this.deleteList.length == 0) {
        this.$message({
          message: "请先选择要删除的项",
          type: "warning"
        });
      } else {
        this.$confirm("是否确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          deleteCompanyCustomer(this.deleteList.join(",")).then(res => {
            if (res.code == 200) {
              that.$message({
                type: "success",
                message: "删除成功!"
              });
              setTimeout(() => {
                that.getlist();
              }, 500);
            }
          });
        });
      }
    },
    getlist() {
      if (
        (this.searchForm.beginTime && !this.searchForm.endTime) ||
        (!this.searchForm.beginTime && this.searchForm.endTime)
      ) {
        return this.$message.warning("请填写开始日期或者结束日期哦");
      } else {
        console.log("111");
      }

      if (this.searchForm.beginTime && this.searchForm.endTime) {
        if (!timeFun(this.searchForm.beginTime, this.searchForm.endTime)) {
          return this.$message.warning("结束时间必须大于开始时间");
        } else {
          console.log("222");
        }
      }
      this.getCompanyCustomerList();
    },
    getCompanyCustomerList() {
      this.listLoading = true;
      getCompanyCustomerList(this.searchForm)
        .then(res => {
          this.tableData = res.data.list;
          this.total = res.data.total;
        })
        .finally(() => {
          this.listLoading = false;
        });
    },
    searchList() {
      this.searchForm.pageNum = 1;
      this.getlist();
    },
    resetList() {
      this.searchForm = {
        taxpayerName: "",
        creditCode: "",
        legalPersonName: "",
        adminId: "",
        provinceId: "",
        beginTime: "",
        beginTime: "",
        pageNum: 1,
        pageSize: 10
      };
      this.getlist();
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  height: calc(100vh - 50px);
  background-color: #f5f5f5;
  position: relative;
}

.el-form-item--mini.el-form-item,
.el-form-item--small.el-form-item {
  // display: flex;
  // align-items: center;
  // margin-right: 20px;
  // white-space: nowrap;
}
</style>
